<?php
$T->loadTpl("./html/".$template."/".$qname.".html");
$T->SetTpl('cssjs','html/cssjs.inc'); 
$T->SetTpl('jsmodel','html/jsmodel.inc');

function Recombination($arr,$id='id',$name='name'){
    $newArr = array();
    foreach($arr as $val){
        $newArr[$val[$id]] = $val[$name];// = $val['id'];
    }
    return $newArr;
}
//                  0       1     2       3       4        5      6       7       8       9       10
$REPAIR_STATUS = ['全部','已申请','修复中','已维修','已结束','待评价','已评价','已撤回','待维修','已拒绝','无法维修'];
$EDIT_REPAIR_STATUS_USER = [
    "2"=>"修复中",
    "3"=>"已维修",
    "10"=>"无法维修"
];
$EDIT_REPAIR_STATUS_ADMIN = [
    "2"=>"修复中",
    "3"=>"已维修",
    "4"=>'已结束',
    "5"=>'待评价',
    "6"=>'已评价',
    "8"=>'待维修',
    "10"=>"无法维修"
];
$status_html = "<option value=''>--修改状态--</option>";
$id=isset($_GET["id"])?$_GET["id"]:"0";
if($id!="0") {
    $user_list = $T->db->query("SELECT id,deptname FROM sys_department")->fetchAll(PDO::FETCH_ASSOC);
    $userlist = Recombination($user_list,'id','deptname');
    $repair_type = $T->db->query("select id,name from sys_equipment_repair where is_flag_delete = 0")->fetchAll(PDO::FETCH_ASSOC);
    $arrlist =  Recombination($repair_type,'id','name');
    $dict = $T->db->query("SELECT * FROM sys_takes_list where id = ".$id)->fetch(PDO::FETCH_ASSOC);

    if($dict["takes_user"] == '待指定'){
        $takes_user = "待指定";
    }else{
        $member = $T->db->query("select id,truename from sys_member")->fetchAll(PDO::FETCH_ASSOC);
        $member_list =  Recombination($member,'id','truename');
        $takes_user = $member_list[$dict["takes_user"]];
    }
    if($dict["agent"] == ""){
        $dict["agent"] = "无";
    }




    $dict["evaluate"] = $dict["evaluate"]."<br/>".$dict["add_evaluate"];
    //print_r($takes_user);exit;
    $T->Set("id",$dict["id"]);
    $T->Set("release_user", $dict["release_user"]);
    $T->Set("agent", $dict["agent"]);
    $T->Set("takes_type", $arrlist[$dict["takes_type"]]);
    $T->Set("code", $dict["code"]);
    $T->Set("takes_user", $takes_user);
    $T->Set("contact_phone", $dict["contact_phone"]);
    $T->Set("release_time", $dict["release_time"]);
    $T->Set("takes_time", $dict["takes_time"]);
    $T->Set("project_type", $dict["project_type"]);
    $T->Set("release_user_id", $dict["release_user_id"]);
    $T->Set("department", $userlist[$dict["department"]]);
    $T->Set("takes_address", $dict["takes_address"]);
    $T->Set("status", $REPAIR_STATUS[$dict["status"]]);
    $T->Set("status_id", $dict["status"]);
    $T->Set("evaluate", $dict["evaluate"]);
    $T->Set("add_evaluate", $dict["add_evaluate"]);
    $T->Set("takes_code", $dict["takes_code"]);
    $T->Set("refuse_reason", $dict["refuse_reason"]);

    $html_show_center = "";
    switch($dict["status"]){
        case "9":

                $sql = $T->db->query("select * from sys_takes_codes WHERE takes_id=".$id." and takes_stauts = 9")->fetchAll(PDO::FETCH_ASSOC);
                $out_yy = "";
                foreach($sql as $val){
                    $out_yy.=$member_list[$val["takes_user_id"]]."&nbsp;&nbsp;&nbsp;&nbsp;".$val["takes_code"]."<br/>";
                }
                $html_show_center.= '<tr id="evaluate_tr_content"><td>拒绝人和拒绝原因</td>';
                $html_show_center.= '<td colspan="5">'.$out_yy.'</td></tr>';
            break;
        default:

                $sql = $T->db->query("select * from sys_takes_codes WHERE takes_id=" . $id . " and takes_stauts = '" . $dict["status"] . "' and takes_user_id='" . $_SESSION['uid'] . "'")->fetch(PDO::FETCH_ASSOC);

                /*判断是否为管理员*/
                if($_SESSION['role'] == 5){
                    $EDIT_REPAIR_STATUS = $EDIT_REPAIR_STATUS_ADMIN;
                    $code = $dict["remarks"];
                    $button_status = "btn_takes_code_admin";
                }else {
                    $EDIT_REPAIR_STATUS = $EDIT_REPAIR_STATUS_USER;
                    $code = $sql['takes_code'];
                    $button_status = "btn_takes_code";
                }
                $repair_status = $dict["status"];
                foreach($EDIT_REPAIR_STATUS as $key=>$val){
                    if($key == $repair_status){
                        $status = "selected";
                    }else{
                        $status = "";
                    }
                    $status_html.="<option value='".$key."'".$status.">".$val."</option>";
                }

                $html_show_center .= '<tr><td>修改维修状态</td>';
                $html_show_center .= '<td colspan="5"><div class="layui-form">';
                $html_show_center .= '<div class="layui-input-inline">';
                $html_show_center .= '<select id="edit_status" name="edit_status" lay-filter="edit_status">';
                $html_show_center .= $status_html . '</select>';
                $html_show_center .= '</div></div></tr></td></tr>';
                $html_show_center .= '<tr id="evaluate_tr"><td>备注</td><td colspan="5">';
                $html_show_center .= '<textarea class="layui-textarea" value="" name="takes_code" id="takes_code" placeholder="维修备注">' .$code. '</textarea>';
                $html_show_center .= '</td></tr>';

                $html_show_center .= '<tr><td></td>';
                $html_show_center .= '<td colspan="6"><input type="button" id="'.$button_status.'" data-id="' . $id . '" lay-submit lay-filter="formDemo" value="备注" class="layui-btn layui-btn-normal" />';
                $html_show_center .= '</td></tr>';

            break;

    }


}else{
    $T->Set("id","");
}
$T->Set("html_show_center",$html_show_center);
//$T->SetBlock("list_department","SELECT id,deptname FROM sys_department");
//$T->SetBlock("repair_type","SELECT * FROM sys_equipment_repair where is_flag_delete = 0 order by id desc");
$T->Set("template",$template);         
$T->clearNaN();       
$T->clearNoN();
$T->display();